/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.product;

import static com.opensymphony.xwork2.Action.INPUT;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;
import database.ProductDAO;
import database.ProductManagerDAO;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import model.Category;
import model.Product;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;

/**
 *
 * @author Trang
 */
@Namespace("/Administration/Product")
public class ProductManagerAction extends ActionSupport implements ModelDriven<Product>, ServletRequestAware{
    private List<Category> categories;
    private Product product;
    private String productCode;
    private File userImage;
    private String userImageContentType;
    private String userImageFileName;
    private HttpServletRequest servletRequest;
    private String imageurl;
    public ProductManagerDAO productDAO = new ProductManagerDAO();
    public ProductDAO productDao = new ProductDAO();
    private Map<Integer,String> sorts;
    private Map<Integer,String> allCategory;
    private Integer idCategory;
    
    public ProductManagerAction()
    {
         allCategory = new HashMap<Integer, String>();
         categories = productDAO.getChildrenCategory();
         for(Category item: categories)
            {
                allCategory.put(item.getIdCategory(), item.getName());
            }        
    }

    public Integer getIdCategory() {
        return idCategory;
    }

    public void setIdCategory(Integer idCategory) {
        this.idCategory = idCategory;
    }

    
    
    public Map<Integer, String> getAllCategory() {
        return allCategory;
    }

    public void setAllCategory(Map<Integer, String> allCategory) {
        this.allCategory = allCategory;
    }

    public Map<Integer, String> getSorts() {
        return sorts;
    }

    public void setSorts(Map<Integer, String> sorts) {
        this.sorts = sorts;
    }
    
    public List<Category> getCategories() {
        return categories;
    }

    /**
     * @param categories the categories to set
     */
    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    /**
     * @return the product
     */
    public Product getProduct() {
        return product;
    }

    /**
     * @param product the product to set
     */
    public void setProduct(Product product) {
        this.product = product;
    }

    
    public String getProductCode() {
        return productCode;
    }

    /**
     * @param productCode the productCode to set
     */
    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }

    @Override
    @VisitorFieldValidator(message = "", appendPrefix = false)
    public Product getModel() {
        return product;
    }

    

    /**
     * @return the userImageContentType
     */
    public String getUserImageContentType() {
        return userImageContentType;
    }

    /**
     * @param userImageContentType the userImageContentType to set
     */
    public void setUserImageContentType(String userImageContentType) {
        this.userImageContentType = userImageContentType;
    }

    /**
     * @return the userImageFileName
     */
    public String getUserImageFileName() {
        return userImageFileName;
    }

    /**
     * @param userImageFileName the userImageFileName to set
     */
    public void setUserImageFileName(String userImageFileName) {
        this.userImageFileName = userImageFileName;
    }
    
    public void setServletRequest(HttpServletRequest servletRequest) {
    this.servletRequest = servletRequest;
 }

    /**
     * @return the userImage
     */
    public File getUserImage() {
        return userImage;
    }

    /**
     * @param userImage the userImage to set
     */
    public void setUserImage(File userImage) {
        this.userImage = userImage;
    }
    
    
   
     @Action(value = "upload-file", results = {
        @Result(name = "success",location = "/manageProduct/w_addproductInfor.jsp"),
              @Result(name = "input", location = "/manageProduct/w_addproduct.jsp")
    })
     public String action1() {
        
         try { 
            System.out.println("Trang");
            String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
            String newPath = filePath.substring(0, filePath.length()-10) + "web/image/";
            
            setImageurl(userImageFileName);
            
            Map session = ActionContext.getContext().getSession();
            session.put("currentImage", userImageFileName);
            
            File fileToCreate = new File(newPath, this.userImageFileName);
            FileUtils.copyFile(this.getUserImage(), fileToCreate);
            addActionError("Upload Image successful!");
            System.out.println(fileToCreate.getAbsolutePath());
           
            allCategory = new HashMap<Integer, String>();
            categories = productDAO.getChildrenCategory();
            for(Category item: categories)
               {
                   allCategory.put(item.getIdCategory(), item.getName());
               }  
            //System.out.println("Imageurl" +imageurl);    
           
        } catch (Exception e) {
            e.printStackTrace();
            addActionError(e.getMessage());
 
            return SUCCESS;
        }
        return SUCCESS;
    }
    

    /**
     * @return the imageurl
     */
    public String getImageurl() {
        return imageurl;
    }

    /**
     * @param imageurl the imageurl to set
     */
    public void setImageurl(String imageurl) {
        this.imageurl = imageurl;
    }
    
}
